//
//  LightViewController.swift
//  Example
//
//  Created by xx on 2025/3/24.
//

import UIKit
import FastSwiftSDK

class LightViewController: FastViewController {
    
    var device:XXDevice?
    
    private lazy var btn_gradient : FastButton = {
        let btn = PublicView.button(title: "渐变", style: .borderColor) {[weak self] in
            self?.sendMsg(type: 1)
        }
        self.view.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_every20th : FastButton = {
        let btn = PublicView.button(title: "跳动", style: .borderColor) { [weak self] in
            self?.sendMsg(type: 2)
        }
        self.view.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_firstToLast : FastButton = {
        let btn = PublicView.button(title: "跑马灯", style: .borderColor) { [weak self] in
            self?.sendMsg(type: 3)
        }
        self.view.addSubview(btn)
        return  btn
    } ()
    
    private lazy var colorWheel : ColorWheelView = {
        let view =  ColorWheelView.init()
        view.callBack = { [weak self] color in
            self?.sendMsg(type: 0, color: color)
        }
        self.view.addSubview(view)
        return  view
    } ()
    
    private lazy var cololView : CololView = {
        let view =  CololView.init()
        view.callBack = { [weak self] color in
            self?.colorWheel.backgroundColor = .hex(color as NSString)
            self?.sendMsg(type: 0, color: color)
        }
        self.view.addSubview(view)
        return  view
    } ()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewDidLayoutSubviews() {
        colorWheel.snp.makeConstraints { make in
            make.top.equalTo(kNavH)
            make.left.equalTo(10)
            make.size.equalTo(300)
        }
        cololView.snp.makeConstraints { make in
            make.top.equalTo(colorWheel)
            make.left.equalTo(colorWheel.snp.right).offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(200)
        }
        btn_gradient.snp.makeConstraints { make in
            make.top.equalTo(cololView.snp.bottom).offset(10)
            make.left.equalTo(cololView)
            make.width.equalTo(60)
            make.height.equalTo(40)
        }
        btn_every20th.snp.makeConstraints { make in
            make.top.equalTo(btn_gradient)
            make.left.equalTo(btn_gradient.snp.right).offset(10)
            make.width.equalTo(btn_gradient)
            make.height.equalTo(btn_gradient)
        }
        btn_firstToLast.snp.makeConstraints { make in
            make.top.equalTo(btn_gradient)
            make.left.equalTo(btn_every20th.snp.right).offset(10)
            make.width.equalTo(btn_gradient)
            make.height.equalTo(btn_gradient)
        }
        
    }
    override func viewWillAppear(_ animated: Bool) {
        if XXDeviceManager.shared.deviceList.count > 0 {
            self.device = XXDeviceManager.shared.deviceList[0]
        }else{
            self.pop()
        }
       
    }
    
    func sendMsg(type:Int,color:String = "#ffffff"){
        if let name = self.device?.type,let mac = self.device?.mac{
            XXMQTT.shared.send(topic:"\(name)/\(mac)/L" , msg: "\(type)/\(color)")
        }
    }


}

